﻿using LituoPro.constant;
using LituoPro.ltcore.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LituoPro.ltcore.service
{
    public class SendDateTimeService
    {

        public Byte[] getSendByte()
        {
            List<byte> byteList = new List<byte>(SystemConstant.START_BYTE);
            byteList.AddRange(getSendDataLength());
            byteList.AddRange(SystemConstant.VER_BYTE);
            byteList.AddRange(SystemConstant.TABLE_BYTE);
            byteList.AddRange(SystemConstant.PAGE_BYTE);
            byteList.AddRange(SystemConstant.CHANNEL_BYTE);
            byteList.AddRange(SystemConstant.SEND_CPU_ID_BYTE);
            byteList.AddRange(Generate8ByteUuid(8));
            byteList.AddRange(getReqSendTimeByte());
            byte[] combinedArray = byteList.ToArray();
            DataVerify dataVerify = new DataVerify();
            byte verify = dataVerify.getVerify(combinedArray);
            byteList.Add(verify);
            byteList.AddRange(SystemConstant.SEND_END_BYTE);
            return byteList.ToArray();
        }

        private Byte[] getSendDataLength()
        {
            List<byte> byteList = new List<byte>();
            byteList.AddRange(SystemConstant.SEND_DATA_LENGHT_BYTE);
            byteList.AddRange(SystemConstant.VER_BYTE);
            byteList.AddRange(SystemConstant.TABLE_BYTE);
            byteList.AddRange(SystemConstant.PAGE_BYTE);
            byteList.AddRange(SystemConstant.CHANNEL_BYTE);
            byteList.AddRange(SystemConstant.SEND_CPU_ID_BYTE);
            byteList.AddRange(Generate8ByteUuid(8));
            byteList.AddRange(getReqSendTimeByte());
            byte[] combinedArray = byteList.ToArray();
            DataVerify dataVerify = new DataVerify();
            byte verify = dataVerify.getVerify(combinedArray);
            byteList.Add(verify);
            byteList.AddRange(SystemConstant.SEND_END_BYTE);
            int dataLength = byteList.Count;
            byte[] byteArray = new byte[2];
            byteArray[0] = (byte)(dataLength >> 8); // 获取高8位并转换为byte类型赋值给第一个元素
            byteArray[1] = (byte)dataLength; // 获取低8位并转换为byte类型赋值给第二个元素
            return byteArray;

        }


        private Byte[] getReqSendTimeByte()
        {
            ReqSendTimeEntity reqSendTimeEntity = new ReqSendTimeEntity();
            reqSendTimeEntity.dataLenghtByte = SystemConstant.DATA_LENGTH_BYTE;
            reqSendTimeEntity.claInsByte = SystemConstant.CLA_INS   ;
            reqSendTimeEntity.p1p2Byte = SystemConstant.P1_P2 ;
            reqSendTimeEntity.lcByte = SystemConstant.LC;
            reqSendTimeEntity.dateTimeByte = getDateTimeByte();
            DataVerify dataVerify = new DataVerify();
            List<byte> byteList = new List<byte>(reqSendTimeEntity.dataLenghtByte);
            byteList.AddRange(reqSendTimeEntity.claInsByte);
            byteList.AddRange(reqSendTimeEntity.p1p2Byte);
            byteList.AddRange(reqSendTimeEntity.lcByte);
            byteList.AddRange(reqSendTimeEntity.dateTimeByte);
            byte[] combinedArray = byteList.ToArray();
            byte verify = dataVerify.getVerify(combinedArray);
            reqSendTimeEntity.checkSumByte = verify;
            byteList.Add(verify);
            return byteList.ToArray();
        }



        private Byte[] Generate8ByteUuid( int length)
        {
            Guid fullGuid = Guid.NewGuid();
            byte[] fullGuidBytes = fullGuid.ToByteArray();
            byte[] byteUuid = new byte[length];
            Array.Copy(fullGuidBytes, 0, byteUuid, 0, length);
            return byteUuid;
        }

        private Byte[] getDateTimeByte() 
        {
            DateTime now = DateTime.Now;
            string year = now.Year.ToString();
            string year1 = year.Substring(0, 2);
            string year2 = year.Substring(2, 2);
            int nYea1 = Convert.ToInt32(year1, 16);
            int nYea2 = Convert.ToInt32(year2, 16);
            int month = Convert.ToInt32(now.Month.ToString(), 16); 
            int day = Convert.ToInt32(now.Day.ToString(), 16);
            int hour = Convert.ToInt32(now.Hour.ToString(), 16);
            int minute = Convert.ToInt32(now.Minute.ToString(), 16);
            int second = Convert.ToInt32(now.Second.ToString(), 16);

            byte[] byteArray = new byte[7];
            byteArray[0] = (byte)(nYea1);
            byteArray[1] = (byte)(nYea2);
            byteArray[2] = (byte)(month);
            byteArray[3] = (byte)(day);
            byteArray[4] = (byte)(hour);
            byteArray[5] = (byte)(minute);
            byteArray[6] = (byte)(second);
            return byteArray;
        }


    }
}
